<template>
  <view>
    <view
      v-for="item in goodsList"
      :key="item.goods_id"
      @click="goToGoodsDetail(item)"
    >
      <my-goods :goods="item" class="z-index" />
    </view>
  </view>
</template>

<script>
import MyGoods from '@/components/my-goods'
export default {
  components: {
    MyGoods
  },
  data() {
    return {
      queryObj: {
        query: "",
        cid: "",
        pagenum: 1,
        pagesize: 10,
      },
      goodsList: [],
      total: 0,
      isLoadingMore: false, //控制是否在上拉加载
    };
  },
  onLoad(options) {
    this.queryObj.query = options.query || "";
    this.queryObj.cid = options.cid || "";
    this.getGoodsListData();
  },
  onPullDownRefresh() {
    //必须在页面或者全局配置中开启下拉刷新才能使用
    console.log("---onPullDownRefresh---");
    this.goodsList = [];
    this.queryObj.pagenum = 1;
    this.total = 0;
    this.getGoodsListData(true);
  },
  onReachBottom() {
    //上拉加载，页必须要在全局配置中开启才可以使用
    console.log("---onReachBottom---");
    if (this.isLoadingMore) {
      return;
    }
    if (this.goodsList.length >= this.total) {
      uni.showToast({
        title: "没有更多数据了",
        icon: "none",
      });
      return;
    }
    this.queryObj.pagenum++;
    this.getGoodsListData();
  },
  methods: {
    async getGoodsListData(isPullDown = false) {
      this.isLoadingMore = true;
      const res = await uni.$request({
        url: "goods/search",
        data: this.queryObj,
        isPullDown,
      });
      if (res.meta.status === 200) {
        this.isLoadingMore = false;
        this.goodsList = [...this.goodsList, ...res.message.goods];
        this.total = res.message.total;
        if (isPullDown) {
          uni.stopPullDownRefresh();
        }
      }
    },
    goToGoodsDetail(item) {
      uni.navigateTo({
        url: "/subpkg/goods-detail/index?goods_id=" + item.goods_id,
      });
    },
  },
};
</script>

<style>
</style>